<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Controller;

/**
 * 后台用户控制器
 * 
 * @author jry <598821125@qq.com>
 */
class ProjectController extends AdminController
{
    // 项目信息列表
    public function index()
    {
        // 搜索
        $keyword = I('keyword', '', 'string');
        $condition = array(
            'like',
            '%' . $keyword . '%'
        );
        $map['project_number|project_name|status'] = array(
            $condition,
            $condition,
            $condition,
            '_multi' => true
        );
        
        $keywords = I('keywords');
        if (! empty($keywords)) {
            $map['status'] = $keywords;
        }
        
        $customerid = I('customerid');
        if (! empty($customerid)) {
            $map['customerid'] = $customerid;
        }
        // 获取所有用户
        
        $data_list = D('Project')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new \Common\Util\Page(D('Project')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
        
        foreach ($data_list as $key => &$val) {
            $vall['customerid'] = $val['customerid'];
            $val['customer_service_id'] = get_employee_info($val['customer_service_id'], 'name');
            $val['customerid'] = get_customer_info($val['customerid'], 'customer_name');
            
            // $item['title'] = '<a href="'.U('Document/index', array('cid' => $item['id'])).'">'.$item['title'].'</a>';
            $val['customerid'] = '<a href="' . U('Customer/xq', array(
                'id' => $vall['customerid']
            )) . '" style="text-decoration:none;">' . $val['customerid'] . '</a>';
            if($val['responsible_person']){
                $developer =json_decode($val['developer']) ;
                $developer_txt='';
                foreach ($developer as $val2){
                    if(empty($developer_txt)){
                        $developer_txt= get_employee_info($val2, 'name');
                    }
                    else{
                        $developer_txt.= ','.get_employee_info($val2, 'name');
                    }
                }
                $val['responsible_person'] = '<a href="#" data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="'.$developer_txt.'">'.get_employee_info($val['responsible_person'], 'name').'</a>';
            }
            $val['contract_start_end_time'] = date('Y/m/d', $val['contract_start_time']) . '~' . date('Y/m/d', $val['contract_end_time']);
            $val['status_text']=get_descriptions_title('project_status', 'project_status', $val['status']);
           
        }
        
        $attre['title'] = '编';
        $attre['class'] = 'label label-primary';
        $attre['href'] = U('Admin/project/edit', array(
            'id' => '__data_id__'
        ));
        
        $attrf['title'] = '服';
        $attrf['class'] = 'label label-tf';
        $attrf['href'] = U('Admin/CustomerService/index', array(
            'projectid' => '__data_id__'
        ));
        
        $attrx['title'] = '任';
        $attrx['class'] = 'label label-tx';
        $attrx['href'] = U('Admin/Task/index', array(
            'projectid' => '__data_id__'
        ));
        
        $btn = array('1'=>'开始开发','2'=>'开发完成','3'=>'测试完成','4'=>'完成','5'=>'结款');
        $attr['field'] = 'status';
        foreach ($btn as $key=>$valBtn){
            
            $attr[$key]['title'] = '&nbsp;'.$valBtn.'&nbsp;';
            $attr[$key]['class'] = 'label label-xq';
            $attr[$key]['href'] = U('Admin/project/status', array(
                'id' => '__data_id__'
            ));
        }

        $project_status = D('Descriptions')->where("tablename='project_status'")->select();
        
        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('项目信息列表')
            ->addTopButton('addnew') // 设置页面标题
            ->addTopButton('delete') // 添加新增按钮
           
            
            ->addTopButton('self',array('title' => '我的项目','href' => U('Admin/project/my') )) 
  
            ->setSearch('请输入项目编号/项目名称', U('index'), $project_status) // 添加删除按钮

            ->addTableColumn('project_number', '编号')
            ->addTableColumn('project_name', '名称')
            ->addTableColumn('customerid', '客户')
            ->addTableColumn('responsible_person', '负责人')
            ->addTableColumn('customer_service_id', '客服')
            ->addTableColumn('contract_start_end_time', '合同起止时间')
            ->addTableColumn('status_text', '状态')
            ->addTableColumn('total_amount', '总金额')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list)
            ->setTableDataPage($page->show())
            ->addRightButton('self', $attre)
            ->addRightButton('self', $attrf)
            ->addRightButton('self', $attrx)
            ->addRightButton('self', array('title' => '添&nbsp;服','class' => 'label label-fw','href' => U('Admin/customerService/add', array('projectid' => '__data_id__')) ))
            ->addRightButton('self', array('title' => '添&nbsp;任','class' => 'label label-rw','href' => U('Admin/task/add', array('projectid' => '__data_id__'))))
            ->addRightButton('caseButton', '', $attr)
            ->display();
    }

    public function my()
    {
        // 搜索
        $keyword = I('keyword', '', 'string');
        $condition = array(
            'like',
            '%' . $keyword . '%'
        );
        $map['project_number|project_name|status'] = array(
            $condition,
            $condition,
            $condition,
            '_multi' => true
        );
        
        $keywords = I('keywords');
        if (! empty($keywords)) {
            $map['status'] = $keywords;
        }
        
        $customerid = I('customerid');
        if (! empty($customerid)) {
            $map['customerid'] = $customerid;
        }
        $uid = is_login();
        
       /*判断是否为项目负责人*/
       $resa=M("UserGroup")->where(array("responsible_person"=>$uid))->where("id>1")->getField("id",true);
      /* echo M()->getlastSql();
       print_r($resa);exit;*/
       
        if($resa){
        	$uid=M("Employee")->where(array("department"=>array("in",$resa)))->getField("uid",true);
        	$map['developer'] = array("in",$uid);
        }else{
        	 $map['developer|customer_service_id'] = array($uid,$uid,'_multi' => true);
        }
        /*判断是否为项目负责人*/
        
        
        
       // $map['developer|customer_service_id'] = array($uid,$uid,'_multi' => true);
        // 获取所有用户
        
        $data_list = D('Project')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('Project')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
        
        foreach ($data_list as $key => &$val) {
            $vall['customerid'] = $val['customerid'];
            $val['developer'] = get_employee_info($val['developer'], 'name');
            $val['customerid'] = get_customer_info($val['customerid'], 'customer_name');
            
            // $item['title'] = '<a href="'.U('Document/index', array('cid' => $item['id'])).'">'.$item['title'].'</a>';
            $val['customerid'] = '<a href="' . U('Customer/xq', array(
                'id' => $vall['customerid']
            )) . '" style="text-decoration:none;">' . $val['customerid'] . '</a>';
            $val['customer_service_id'] = get_employee_info($val['customer_service_id'], 'name');
            $val['contract_start_end_time'] = date('Y/m/d', $val['contract_start_time']) . '~' . date('Y/m/d', $val['contract_end_time']);
            if($val['responsible_person']){
                $val['responsible_person'] = get_employee_info($val['responsible_person'], 'name');
            }
            if ($val['status'] == 1) {
                $val['project_status'] = 1;
            } elseif ($val['status'] == 2) {
                $val['project_status'] = 2;
            } elseif ($val['status'] == 3) {
                $val['project_status'] = 3;
            } elseif ($val['status'] == 4) {
                $val['project_status'] = 4;
            } elseif ($val['status'] == 5) {
                $val['project_status'] = 5;
            }
        }
        
        $attre['title'] = '编';
        $attre['class'] = 'label label-primary';
        $attre['href'] = U('Admin/project/edit', array(
            'id' => '__data_id__'
        ));
        
        $attrx['title'] = '任';
        $attrx['class'] = 'label label-tx';
        $attrx['href'] = U('Admin/Task/my', array(
            'projectid' => '__data_id__'
        ));
        
        $attr['field'] = 'project_status';
        $attr[1]['title'] = '&nbsp;开发&nbsp;';
        $attr[1]['class'] = 'label label-xq';
        $attr[1]['href'] = U('Admin/project/status', array(
            'id' => '__data_id__'
        ));
        
        $attr['field'] = 'project_status';
        $attr[2]['title'] = '&nbsp;测试&nbsp;';
        $attr[2]['class'] = 'label label-xq';
        $attr[2]['href'] = U('Admin/project/status', array(
            'id' => '__data_id__'
        ));
        
        $attr['field'] = 'project_status';
        $attr[3]['title'] = '&nbsp;完成&nbsp;';
        $attr[3]['class'] = 'label label-xq';
        $attr[3]['href'] = U('Admin/project/status', array(
            'id' => '__data_id__'
        ));
        
        $attr['field'] = 'project_status';
        $attr[4]['title'] = '&nbsp;结束&nbsp;';
        $attr[4]['class'] = 'label label-xq';
        $attr[4]['href'] = U('Admin/project/status', array(
            'id' => '__data_id__'
        ));
        
        //print_r($attr);
        
        $project_status = D('Descriptions')->where("tablename='project_status'")->select();
        
        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('项目信息列表')
            ->addTopButton('addnew') // 设置页面标题

            ->addTopButton('delete') // 添加新增按钮
                                     // ->addTopButton('resume') //添加启用按钮
                                     // ->addTopButton('forbid') //添加禁用按钮               
       // ->addTopButton('self',array('title' => '我的任务','href' => U('Admin/task/my') )) 
        //    ->addTopButton('self',array('title' => '我的服务','href' => U('Admin/customer_service/my') )) 
            ->addTopButton('self',array('title' => '我的项目','href' => U('Admin/project/my') )) 
 
            ->setSearch('请输入项目编号/项目名称', U('index'), $project_status) // 添加删除按钮

            ->addTableColumn('project_number', '编号')
            ->addTableColumn('project_name', '名称')
            ->addTableColumn('customerid', '客户')
            ->addTableColumn('responsible_person', '负责人')
            ->addTableColumn('customer_service_id', '客服')
            ->
        // ->addTableColumn('contract_start_time','合同开始时间')
        // ->addTableColumn('contract_end_time','合同结束时间')
        // ->addTableColumn('real_start_time','实际开始时间')
        // ->addTableColumn('real_end_time','实际结束时间')
        addTableColumn('contract_start_end_time', '合同起止时间')
            ->
        // ->addTableColumntime('real_start_time','real_end_time','实际合同起止时间')
        
        addTableColumn('total_amount', '总金额')
            ->
        // ->addTableColumn('create_time', '创建时间','time')
        
        // ->addTableColumn('status', '状态', 'status')
        addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list)
            ->setTableDataPage($page->show()) // 数据列表

            ->addRightButton('self', $attre) // 数据列表分页
                                              // ->addRightButton('edit') //添加编辑按钮
        
            ->addRightButton('self', $attrx) // 添加编辑按钮
                                            // ->addRightButton('self',$attrf)
        
            ->addRightButton('self', array('title' => '添&nbsp;任','class' => 'label label-rw','href' => U('Admin/task/myadd', array('projectid' => '__data_id__'))))
        // ->addRightButton('delete') //添加删除按钮
        // ->addRightButton('self',array('title'=>'添&nbsp;服','class'=>'label label-fw','href'=>U('Admin/customerService/add', array('projectid' => '__data_id__')))) //添加删除按钮
        
            ->addRightButton('caseButton', '', $attr) // 添加删除按钮

            ->display();
    }
    // 新增客户信息
    public function add()
    {
        $customerid = I('customerid');
        // if(empty($customerid)){
        // $customerid='00';
        // }
        if (IS_POST) {
            $developer = I("developer");
            $_POST['developer']=json_encode($developer);
            $_POST['contract_start_time'] = strtotime($_POST['contract_start_time']);
            $_POST['contract_end_time'] = strtotime($_POST['contract_end_time']);
            $project_object = D('Project');
            $data = $project_object->create();
            if ($data) {
                $id = $project_object->add();
                if ($id) {
                    $this->success('新增成功', U('my'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($project_object->getError());
            }
        } else {
            $kefu_arr = M("employee")->where(array("department"=>7,"status"=>1))->field("uid as id,name as title")->select();
            $kefu_arr = arr_to_option($kefu_arr);
            $kaifa_arr = M("employee")->where(array("department"=>array("in","3,4,5"),"status"=>1))->field("uid as id,name as title")->select();
            $kaifa_arr = arr_to_option($kaifa_arr);
            $customer = get_customer_info($customerid, 'customer_name');
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增项目信息')
                ->setPostUrl(U('add'))// 设置页面标题

                ->addFormItem('reg_type', 'hidden', '注册方式', '注册方式') // 设置表单提交地址

                ->addFormItem('project_number', 'text', '项目编号', '项目编号')
            // ->addFormItem('usertype', 'radio', '用户类型', '用户类型', C('USER_TYPE_LIST'))
           
                ->addFormItem('project_name', 'text', '项目名称', '项目名称')
                ->addFormItem('customerid', 'user_list', '客户ID', $customer)
                ->addFormItem('responsible_person', 'select', '负责人', '项目主要开发人', $kaifa_arr)
                ->addFormItem('developer', 'checkbox', '开发者', '开发者', $kaifa_arr)
                ->addFormItem('customer_service_id', 'select', '客服', '客服', $kefu_arr)
                ->addFormItem('contract_start_time', 'date', '合同开始时间', '合同开始时间')
                ->addFormItem('contract_end_time', 'date', '合同结束时间', '合同结束时间')
                ->
            // ->addFormItem('real_start_time','date','实际开始时间','实际开始时间')
            // ->addFormItem('real_end_time','date', '实际结束时间', '实际结束时间')
            addFormItem('total_amount', 'text', '总金额', '总金额')
                ->addFormItem('remark', 'textarea', '备注', '备注')
                ->
            // ->addFormItem('vip', 'radio', 'VIP等级', 'VIP等级', C('USER_VIP_LEVEL'))
            
            setFormData(array('customerid' => $customerid,'customer_service_id'=>is_login()))
                ->display();
        }
    }

    public function myadd()
    {
        $customerid = I('customerid');
        // if(empty($customerid)){
        // $customerid='00';
        // }
        if (IS_POST) {
            
            $_POST['contract_start_time'] = strtotime($_POST['contract_start_time']);
            $_POST['contract_end_time'] = strtotime($_POST['contract_end_time']);
            $project_object = D('Project');
            $data = $project_object->create();
            if ($data) {
                $id = $project_object->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($project_object->getError());
            }
        } else {
            $kefu_arr = M("employee")->where(array("department"=>7,"status"=>1))->field("uid as id,name as title")->select();
              $kefu_arr = arr_to_option($kefu_arr);   
                
            $kaifa_arr = M("employee")->where(array("department"=>array("in","3,4,5"),"status"=>1))->field("uid as id,name as title")->select();
              $kaifa_arr = arr_to_option($kaifa_arr);  
                
            $customer = get_customer_info($customerid, 'customer_name');
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增项目信息')
                ->setPostUrl(U('add')) // 设置页面标题

                ->addFormItem('reg_type', 'hidden', '注册方式', '注册方式') // 设置表单提交地址

                ->  addFormItem('project_number', 'text', '项目编号', '项目编号')
            // ->addFormItem('usertype', 'radio', '用户类型', '用户类型', C('USER_TYPE_LIST'))
          
                ->addFormItem('project_name', 'text', '项目名称', '项目名称')
                ->addFormItem('customerid', 'text', '客户ID', $customer)
                ->addFormItem('responsible_person', 'select', '负责人', '项目主要开发人', $kaifa_arr)
                ->addFormItem('developer', 'checkbox', '开发者', '开发者', $kaifa_arr)
                ->addFormItem('customer_service_id', 'select2', '客服ID', '客服ID', $kefu_arr)
                ->addFormItem('contract_start_time', 'date', '合同开始时间', '合同开始时间')
                ->addFormItem('contract_end_time', 'date', '合同结束时间', '合同结束时间')
                ->
            // ->addFormItem('real_start_time','date','实际开始时间','实际开始时间')
            // ->addFormItem('real_end_time','date', '实际结束时间', '实际结束时间')
            addFormItem('total_amount', 'text', '总金额', '总金额')
                ->addFormItem('remark', 'textarea', '备注', '备注')
                ->
            // ->addFormItem('vip', 'radio', 'VIP等级', 'VIP等级', C('USER_VIP_LEVEL'))
            
            setFormData(array(
                'customerid' => $customerid
            ))
                ->display();
        }
    }

    /**
     * 编辑
     * 
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
        // 获取信息
        $info = D('Project')->find($id);
        
        if (IS_POST) {
            
            $_POST['contract_start_time'] = strtotime($_POST['contract_start_time']);
            $_POST['contract_end_time'] = strtotime($_POST['contract_end_time']);
            $project_object = D('Project');
            
            if ($project_object->save($_POST)) {
                $this->success('更新成功', U('index'));
            } else {
                $this->error('更新失败', $project_object->getError());
            }
        } else {
            $kefu_arr = M("employee")->where(array("department"=>7,"status"=>1))->field("uid as id,name as title")->select();
            $kefu_arr = arr_to_option($kefu_arr);
            $kaifa_arr = M("employee")->where(array("department"=>array("in","3,4,5"),"status"=>1))->field("uid as id,name as title")->select();
            $kaifa_arr = arr_to_option($kaifa_arr);
            $info = D('Project')->find($id);
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑')// 设置页面标题
                -> setPostUrl(U('edit'))// 设置表单提交地址
                -> addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('project_number', 'text', '项目编号', '项目编号')
                ->addFormItem('project_name', 'text', '项目名称', '项目名称')
                ->addFormItem('customerid', 'customer', '客户编号', '客户ID')
                ->addFormItem('responsible_person', 'select', '负责人', '项目主要开发人', $kaifa_arr)
                ->addFormItem('developer', 'checkbox', '开发者', '开发者', $kaifa_arr)
                ->addFormItem('customer_service_id', 'select', '客服', '客服', $kefu_arr)
                ->addFormItem('contract_start_time', 'date', '合同开始时间', '合同开始时间')
                ->addFormItem('contract_end_time', 'date', '合同结束时间', '合同结束时间')
                ->addFormItem('total_amount', 'text', '总金额', '总金额')
                ->setFormData($info)
                ->display();
        }
    }

    public function status($id)
    {
        $map['id'] = $id;
        $status = D('Project')->where($map)->getField('status');
        if ($status == 1) {
            $res = D('Project')->where($map)->setField('status', '2');
            D('Project')->where($map)->setField('real_start_time', time());
            if ($res) {
                redirect(U('Project/index'));
            }
        }
        if ($status == 2) {
            $res = D('Project')->where($map)->setField('status', '3');
            if ($res) {
                redirect(U('Project/index'));
            }
        }
        if ($status == 3) {
            $res = D('Project')->where($map)->setField('status', '4');
            if ($res) {
                redirect(U('Project/index'));
            }
        }
        if ($status == 4) {
            $res = D('Project')->where($map)->setField('status', '5');
            D('Project')->where($map)->setField('real_end_time', time());
            if ($res) {
                redirect(U('Project/index'));
            }
        }
    }
}